package com.isp.org.server.domain.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.isp.org.api.dto.role.SysRole;
import com.isp.org.server.domain.entity.RoleUserEntity;
import com.isp.org.server.domain.mapper.RoleUserMapper;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;

/**
 * @author linsy
 * @version 1.0
 * @created 2024/3/13 09:18
 */
@Service
@RequiredArgsConstructor
public class RoleUserService extends ServiceImpl<RoleUserMapper, RoleUserEntity> {

    private final RoleUserMapper roleUserMapper;

    public List<SysRole> findRolesByUserId(String app, Long userId) {
        return roleUserMapper.findRolesByUserId(app, userId);
    }

    public void deleteInRoleId(Long userId, Set<Long> roleIds) {
        roleUserMapper.deleteInRoleId(userId, roleIds);
    }

    public List<RoleUserEntity> queryPage(Long id) {
        return roleUserMapper.queryPage(id);
    }

    public Set<Long> queryByUserId(Long userId) {
        QueryWrapper<RoleUserEntity> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId);
        List<RoleUserEntity> userRoleList = list(queryWrapper);
        return userRoleList.stream().map(RoleUserEntity::getRoleId).collect(Collectors.toSet());
    }
}
